//
//  TabBarViewController.m
//  TabBarViewControlDemo
//
//  Created by mobao on 16/4/1.
//  Copyright © 2016年 mobao. All rights reserved.
//

#import "TabBarViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"


@implementation TabBarViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    [self tabBarView];
    
    [self addSlideGesture];
}


-(void)tabBarView{
    FirstViewController *firstView = [[FirstViewController alloc]init];
    SecondViewController *secondView = [[SecondViewController alloc]init];
    ThirdViewController *thirdView = [[ThirdViewController alloc]init];
    //添加三个子视图
    [self setViewControllers:@[firstView,secondView,thirdView] animated:YES];
    
    //设置选项的文字颜色
    [self.tabBar setTintColor:[UIColor colorWithRed:0 green:192/255.0 blue:243/255.0 alpha:1]];
    
    //tabBar白色背景
    CGRect frame = CGRectMake(0, 0,self.view.frame.size.width , 48);
    UIView *view = [[UIView alloc]initWithFrame:frame];
    [view setBackgroundColor:[UIColor whiteColor]];
    [self.tabBar insertSubview:view atIndex:0];
}

//添加滑动手势
-(void)addSlideGesture{
    //向右
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRight)];
    
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    
    [self.view addGestureRecognizer:swipeLeft];
    
    //向左
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(tappedLeft)];

    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    
    [self.view addGestureRecognizer:swipeRight];
}

-(void)tappedLeft{
    NSLog(@"左");
    NSUInteger selectedIndex = [self selectedIndex];
    if (selectedIndex > 0) {
        UIView *fromView = [self.selectedViewController view];
        UIView *toView = [[self.viewControllers objectAtIndex:selectedIndex-1] view];
        [UIView transitionFromView:fromView toView:toView duration:0.5 options:UIViewAnimationOptionTransitionNone  completion:^(BOOL finished) {
            if (finished) {
                [self setSelectedIndex:selectedIndex - 1];
            }
        }];
    }
    
}

-(void)tappedRight{
    NSLog(@"右");
    NSUInteger selectedIndex = [self selectedIndex];
    NSArray *viewControllerArr = self.viewControllers;
    if (selectedIndex < viewControllerArr.count - 1) {
        UIView *fromView = [self.selectedViewController view];
        UIView *toView = [[self.viewControllers objectAtIndex:selectedIndex+1] view];
        [UIView transitionFromView:fromView toView:toView duration:0.5 options:UIViewAnimationOptionTransitionNone completion:^(BOOL finished) {
            if (finished) {
                [self setSelectedIndex:selectedIndex + 1];
            }
        }];
    }
    
}

#pragma mark UITabBarDelegate
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSLog(@"%@",item.title);
}





@end

















